package com.bp.hearthealth.pulse.lnapps.bp_ui.bp_activity.bp_article

import android.view.View
import com.anythink.core.api.ATAdInfo
import com.anythink.core.api.AdError
import com.anythink.interstitial.api.ATInterstitial
import com.anythink.nativead.api.ATNativeAdView
import com.anythink.nativead.api.ATNativePrepareExInfo
import com.anythink.nativead.api.ATNativePrepareInfo
import com.bp.hearthealth.pulse.lnapps.R
import com.bp.hearthealth.pulse.lnapps.ads.admob.interstitial_ad.AdMobInterstitialAdListener
import com.bp.hearthealth.pulse.lnapps.ads.admob.interstitial_ad.AdMobInterstitialAdManager
import com.bp.hearthealth.pulse.lnapps.ads.admob.native_ad.AdMobNativeAdListener
import com.bp.hearthealth.pulse.lnapps.ads.admob.native_ad.AdMobNativeAdManager
import com.bp.hearthealth.pulse.lnapps.ads.model.AdsSwitch
import com.bp.hearthealth.pulse.lnapps.ads.topon.interstitial_ad.TopOnInterAdManager
import com.bp.hearthealth.pulse.lnapps.ads.topon.interstitial_ad.listener.manual_load.TopOnInterstitialAdListener
import com.bp.hearthealth.pulse.lnapps.ads.topon.native_ad.SelfRenderViewUtil
import com.bp.hearthealth.pulse.lnapps.ads.topon.native_ad.TopOnNativeAdListener
import com.bp.hearthealth.pulse.lnapps.ads.topon.native_ad.TopOnNativeAdManager
import com.bp.hearthealth.pulse.lnapps.appsflyer.AppsFlyerUtils
import com.bp.hearthealth.pulse.lnapps.base.BpBaseActivity
import com.bp.hearthealth.pulse.lnapps.bp_extension.LogUtil
import com.bp.hearthealth.pulse.lnapps.bp_extension.UIHelperBp
import com.bp.hearthealth.pulse.lnapps.bp_utils.Constants
import com.bp.hearthealth.pulse.lnapps.databinding.ActivityArticleDetailsBgBinding
import com.google.android.gms.ads.nativead.NativeAd
import com.gyf.immersionbar.ImmersionBar

class BpArticleDetailsActivity : BpBaseActivity<ActivityArticleDetailsBgBinding>() {

    private var type: String = ""
    private var nativeAd: NativeAd? = null
    private var topOnNativeAd: com.anythink.nativead.api.NativeAd? = null

    override fun initData() {
        super.initData()
        binding.lifecycleOwner = this
//        loadAdmobInsertAd()
        if (!AdsSwitch.getBoolean(AdsSwitch.adInterCacheControl)) {
            TopOnInterAdManager.initManualLoadInterstitialAd(this)
            TopOnInterAdManager.manualLoadAd()
        }
        loadTopOnInsertAd()
//        loadNativeAd()
        loadTopOnNativeAd()
        type = intent.getStringExtra("type").toString()
        val title = intent.getStringExtra("title").toString()
        val drawableId = intent.getIntExtra("drawableId", 0)
        if (drawableId != 0) {
            binding.ivHead.setImageResource(drawableId)
//            GlideUtil.loadImage(this, url, binding.ivHead)
        }
        //详情页展示
        if (type == Constants.LIFE_STYLE_ARTICE_FRAGMENT_VIEW) {
            AppsFlyerUtils.trackEvent("lifestyle_content_show")
        }
        binding.tvArticleTitle.text = title
        when (title) {
            UIHelperBp.getString(R.string._blood_pressure_tips_a_str) -> {
                binding.tvArticle.text = UIHelperBp.getString(R.string.tips_blood_pressure_a_str)
            }

            UIHelperBp.getString(R.string._blood_pressure_tips_b_str) -> {
                binding.tvArticle.text = UIHelperBp.getString(R.string.tips_blood_pressure_b_str)
            }

            UIHelperBp.getString(R.string._blood_pressure_tips_c_str) -> {
                binding.tvArticle.text = UIHelperBp.getString(R.string.tips_blood_pressure_c_str)
            }

            UIHelperBp.getString(R.string._blood_pressure_tips_d_str) -> {
                binding.tvArticle.text = UIHelperBp.getString(R.string.tips_blood_pressure_d_str)
            }

            UIHelperBp.getString(R.string._blood_pressure_tips_e_str) -> {
                binding.tvArticle.text = UIHelperBp.getString(R.string.tips_blood_pressure_e_str)
            }

            UIHelperBp.getString(R.string._blood_pressure_tips_f_str) -> {
                binding.tvArticle.text = UIHelperBp.getString(R.string.tips_blood_pressure_f_str)
            }

            UIHelperBp.getString(R.string._blood_sugar_tips_a_str) -> {
                binding.tvArticle.text = UIHelperBp.getString(R.string.tips_blood_sugar_a_str)
            }

            UIHelperBp.getString(R.string._blood_sugar_tips_b_str) -> {
                binding.tvArticle.text = UIHelperBp.getString(R.string.tips_blood_sugar_b_str)
            }

            UIHelperBp.getString(R.string._blood_sugar_tips_c_str) -> {
                binding.tvArticle.text = UIHelperBp.getString(R.string.tips_blood_sugar_c_str)
            }

            UIHelperBp.getString(R.string._blood_sugar_tips_d_str) -> {
                binding.tvArticle.text = UIHelperBp.getString(R.string.tips_blood_sugar_d_str)
            }

            UIHelperBp.getString(R.string._blood_sugar_tips_e_str) -> {
                binding.tvArticle.text = UIHelperBp.getString(R.string.tips_blood_sugar_e_str)
            }

            UIHelperBp.getString(R.string._blood_sugar_tips_f_str) -> {
                binding.tvArticle.text = UIHelperBp.getString(R.string.tips_blood_sugar_f_str)
            }

            UIHelperBp.getString(R.string._healthy_diet_tips_1_str) -> {
                binding.tvArticle.text = UIHelperBp.getString(R.string.tips_healthy_diet_a_str)
            }

            UIHelperBp.getString(R.string._healthy_diet_tips_6_str) -> {
                binding.tvArticle.text = UIHelperBp.getString(R.string.tips_healthy_diet_b_str)
            }

            UIHelperBp.getString(R.string._healthy_diet_tips_2_str) -> {
                binding.tvArticle.text = UIHelperBp.getString(R.string.tips_healthy_diet_c_str)
            }

            UIHelperBp.getString(R.string._healthy_diet_tips_3_str) -> {
                binding.tvArticle.text = UIHelperBp.getString(R.string.tips_healthy_diet_d_str)
            }

            UIHelperBp.getString(R.string._healthy_diet_tips_4_str) -> {
                binding.tvArticle.text = UIHelperBp.getString(R.string.tips_healthy_diet_e_str)
            }

            UIHelperBp.getString(R.string._healthy_diet_tips_5_str) -> {
                binding.tvArticle.text = UIHelperBp.getString(R.string.tips_healthy_diet_f_str)
            }
        }
    }

    private fun loadTopOnInsertAd() {
        TopOnInterAdManager.setManualLoadListener(object : TopOnInterstitialAdListener {
            override fun onFireBaseAdIsClosed() {
                LogUtil.iTopOn("插页广告Firebase中关闭了。。。。。。。。")
                if (_binding != null) {
                    binding.constraint.visibility = View.VISIBLE
                }
            }

            //            override fun onInterstitialAdLoaded() {
//                LogUtil.iTopOn("插页广告 onInterstitialAdLoaded 回调")
//                if (TopOnInterAdManager.isAdReady()) {
//                    TopOnInterAdManager.mInterstitialAd!!.show(this@BpArticleDetailsActivity)
//                } else {
//                    TopOnInterAdManager.setManualLoadListener(null)
//                    binding.constraint.visibility = View.VISIBLE
//                }
//            }
            override fun onInterstitialAdClose(entity: ATAdInfo) {
                TopOnInterAdManager.setManualLoadListener(null)
                binding.constraint.visibility = View.VISIBLE
            }

            override fun onInterstitialAdLoadFail(adError: AdError) {
                TopOnInterAdManager.setManualLoadListener(null)
                binding.constraint.visibility = View.VISIBLE
            }

            override fun onInterstitialAdVideoError(adError: AdError) {
                TopOnInterAdManager.setManualLoadListener(null)
                binding.constraint.visibility = View.VISIBLE
            }

            override fun onNotReady() {
                TopOnInterAdManager.setManualLoadListener(null)
                binding.constraint.visibility = View.VISIBLE
            }

            override fun onInterstitialAdShow(entity: ATAdInfo) {
                when (type) {
                    //BloodPressure Tracker 视图Fragment界跳转
                    Constants.BLOOD_PRESSURE_FRAGMENT -> {

                    }
                    //首页跳转文章详情
                    Constants.HOME_FRAGMENT_VIEW -> {
                        ATInterstitial.entryAdScenario(Constants.TOP_ON_INTER_ID,"f66471ede8f0a7")
                        LogUtil.iTopOn("TopOn 配置的场景ID为: 【Health页点击Tip入口后_插页广告】")
                        AppsFlyerUtils.trackEvent("ad_inter_health_tips_show")
                    }
                    //文章列表跳转文章详情
                    Constants.LIFE_STYLE_ARTICE_FRAGMENT_VIEW -> {
                        ATInterstitial.entryAdScenario(Constants.TOP_ON_INTER_ID, "f66471fe48adc3")
                        LogUtil.iTopOn("TopOn 配置的场景ID为: 【Lifestyle点击文章入口后_插页广告】")
                        AppsFlyerUtils.trackEvent("ad_inter_lifestyle_content_show")
                    }
                    //血压结果页跳转文章详情
                    Constants.BLOOD_PRESSURE_RESULT_VIEW -> {
                        AppsFlyerUtils.trackEvent("ad_inter_bp_result_tips_show")
                        ATInterstitial.entryAdScenario(Constants.TOP_ON_INTER_ID,"f66471fa6146ef")
                        LogUtil.iTopOn("TopOn 配置的场景ID为: 【血压结果页点击Tips入口后_插页广告】")
                    }
                    //血压历史页
                    Constants.BLOOD_PRESSURE_LIST_RESULT_VIEW -> {
                        ATInterstitial.entryAdScenario(Constants.TOP_ON_INTER_ID,"f66471fc807f3e")
                        LogUtil.iTopOn("TopOn 配置的场景ID为: 【血压History结果页点击Tips入口后_插页广告】")
                        AppsFlyerUtils.trackEvent("ad_inter_bp_history_result_tips_show")
                    }
                    //心率结束结果页页跳转文章详情
                    Constants.HEART_RATE_RESULT_VIEW -> {
                        AppsFlyerUtils.trackEvent("ad_inter_hr_report_tips_show")
                        ATInterstitial.entryAdScenario(Constants.TOP_ON_INTER_ID, "f66471f1e33804")
                        LogUtil.iTopOn("TopOn 配置的场景ID为: 【Heart Rate结果页点击Tips入口后_插页广告】")
                    }
                    //心率列表结束结果页页跳转文章详情
                    Constants.HEART_RATE_LIST_RESULT_VIEW -> {
                        AppsFlyerUtils.trackEvent("ad_inter_hr_history_report_tips_show")
                        ATInterstitial.entryAdScenario(Constants.TOP_ON_INTER_ID, "f66471f3e9bf94")
                        LogUtil.iTopOn("TopOn 配置的场景ID为: 【Heart Rate历史记录页点击Tips入口后_插页广告】")
                    }
                }
            }
        })
        TopOnInterAdManager.showInterstitialAd(this)
    }

    private fun loadAdmobInsertAd() {
        AdMobInterstitialAdManager.setInterstitialAdListener(object : AdMobInterstitialAdListener {
            override fun onAdDismissedFullScreenContent() {
                binding.constraint.visibility = View.VISIBLE
            }

            override fun onAdFailedToShowFullScreenContent() {
                binding.constraint.visibility = View.VISIBLE
            }

            override fun onNotReady() {
                binding.constraint.visibility = View.VISIBLE
            }

            override fun onAdShowedFullScreenContent() {
                when (type) {
                    //BloodPressure Tracker 视图Fragment界跳转
                    Constants.BLOOD_PRESSURE_FRAGMENT -> {

                    }
                    //首页跳转文章详情
                    Constants.HOME_FRAGMENT_VIEW -> {
                        AppsFlyerUtils.trackEvent("ad_inter_health_tips_show")
                        ATInterstitial.entryAdScenario(Constants.TOP_ON_INTER_ID,"f66471ede8f0a7")
                        LogUtil.iTopOn("TopOn 配置的场景ID为: 【Health页点击Tip入口后_插页广告】")
                    }
                    //文章列表跳转文章详情
                    Constants.LIFE_STYLE_ARTICE_FRAGMENT_VIEW -> {
                        AppsFlyerUtils.trackEvent("ad_inter_lifestyle_content_show")
                        ATInterstitial.entryAdScenario(Constants.TOP_ON_INTER_ID,"f66471fe48adc3")
                        LogUtil.iTopOn("TopOn 配置的场景ID为: 【Lifestyle点击文章入口后_插页广告】")
                    }
                    //血压结果页跳转文章详情
                    Constants.BLOOD_PRESSURE_RESULT_VIEW -> {
                        AppsFlyerUtils.trackEvent("ad_inter_bp_result_tips_show")
                        ATInterstitial.entryAdScenario(Constants.TOP_ON_INTER_ID,"f66471fa6146ef")
                        LogUtil.iTopOn("TopOn 配置的场景ID为: 【血压结果页点击Tips入口后_插页广告】")
                    }
                    //血压历史页
                    Constants.BLOOD_PRESSURE_LIST_RESULT_VIEW -> {
                        ATInterstitial.entryAdScenario(Constants.TOP_ON_INTER_ID,"f66471fc807f3e")
                        LogUtil.iTopOn("TopOn 配置的场景ID为: 【血压History结果页点击Tips入口后_插页广告】")
                        AppsFlyerUtils.trackEvent("ad_inter_bp_history_result_tips_show")
                    }
                    //心率结束结果页页跳转文章详情
                    Constants.HEART_RATE_RESULT_VIEW -> {
                        AppsFlyerUtils.trackEvent("ad_inter_hr_report_tips_show")
                        ATInterstitial.entryAdScenario(Constants.TOP_ON_INTER_ID, "f66471f1e33804")
                        LogUtil.iTopOn("TopOn 配置的场景ID为: 【Heart Rate结果页点击Tips入口后_插页广告】")
                    }
                    //心率列表结束结果页页跳转文章详情
                    Constants.HEART_RATE_LIST_RESULT_VIEW -> {
                        AppsFlyerUtils.trackEvent("ad_inter_hr_history_report_tips_show")
                        ATInterstitial.entryAdScenario(Constants.TOP_ON_INTER_ID, "f66471f3e9bf94")
                        LogUtil.iTopOn("TopOn 配置的场景ID为: 【Heart Rate历史记录页点击Tips入口后_插页广告】")
                    }
                }
            }
        })
        AdMobInterstitialAdManager.showInterstitialAd(this)
    }

    /**
     * 加载原生广告
     *     ■ 原生广告资源加载到，展示广告
     *     ■ 原生广告资源未加载到，隐藏广告位
     */
    private fun loadNativeAd() {
        AdMobNativeAdManager.setNativeAdListener(object : AdMobNativeAdListener {
            override fun onAdLoaded(loadedNativeAd: MutableList<NativeAd>) {
                setNativeAdView(loadedNativeAd)
            }

            override fun onAdImpression() {
                //原生广告埋点
                AppsFlyerUtils.trackEvent("ad_native_lifestyle_content_show")
                ATInterstitial.entryAdScenario(Constants.TOP_ON_INTER_ID,"f66472079a350a")
                LogUtil.iTopOn("TopOn 配置的场景ID为: 【Lifestyle详情页_原生广告】")
                AdMobNativeAdManager.removeUsedNativeAdListener()
            }
        })
        val nativeAdList = AdMobNativeAdManager.getNativeAds()
        if (AdMobNativeAdManager.getNativeAds().isNotEmpty()) {
            setNativeAdView(nativeAdList)
        }
    }

    override fun onPause() {
        if (topOnNativeAd != null) {
            topOnNativeAd!!.onPause()
        }
        super.onPause()
    }

    override fun onResume() {
        if (topOnNativeAd != null) {
            topOnNativeAd!!.onResume()
        }
        super.onResume()
    }


    private fun loadTopOnNativeAd() {
        if (TopOnNativeAdManager.getNativeAds().isNotEmpty()) {
            setNativeTopOnAdView()
        } else {
            TopOnNativeAdManager.setNativeAdListener(object : TopOnNativeAdListener{
                override fun onNativeAdLoaded() {
                    LogUtil.iTopOn("TopOn 【原生广告】 监听，显示广告")
                    if (TopOnNativeAdManager.getNativeAds().isNotEmpty()) {
                        setNativeTopOnAdView()
                    }
                }
            })
        }
    }

    private fun setNativeTopOnAdView() {
        topOnNativeAd = TopOnNativeAdManager.getNativeAds().first()
        TopOnNativeAdManager.setNativeAdListener(object : TopOnNativeAdListener {
            override fun onAdImpressed(view: ATNativeAdView, entity: ATAdInfo) {
                TopOnNativeAdManager.removeUsedNativeAdListener()
            }
        })
        binding.nativeAdView.removeAllViews()
        var mNativePrepareInfo: ATNativePrepareInfo? = null
        try {
            mNativePrepareInfo = ATNativePrepareExInfo()
            if (topOnNativeAd?.isNativeExpress == true) {
                LogUtil.iTopOn("【原生广告】 自渲染广告")
                topOnNativeAd?.renderAdContainer(
                    binding.nativeAdView,
                    null
                )
            } else {
                LogUtil.iTopOn("【原生广告】 模板广告")
                SelfRenderViewUtil.bindSelfRenderView(
                    activity,
                    topOnNativeAd?.adMaterial,
                    binding.nativeSelfrenderView.root,
                    mNativePrepareInfo
                )
                topOnNativeAd?.renderAdContainer(
                    binding.nativeAdView,
                    binding.nativeSelfrenderView.root
                )
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        topOnNativeAd?.prepare(binding.nativeAdView, mNativePrepareInfo)
        binding.nativeAdView.visibility = View.VISIBLE
        TopOnNativeAdManager.setNativeAdListener(object : TopOnNativeAdListener {
            override fun onAdCloseButtonClick(view: ATNativeAdView, entity: ATAdInfo) {
                TopOnNativeAdManager.destroyAd()
                binding.nativeAdView.visibility = View.GONE
            }
        })
    }

    private fun setNativeAdView(nativeAdList: MutableList<NativeAd>) {
        nativeAd = nativeAdList.first()
        binding.nativeAdContainer.setNativeAd(nativeAd)
        binding.nativeAdContainer.visibility = View.VISIBLE
    }

    override fun initImmersionBar() {
        super.initImmersionBar()
        binding.ivBack.setOnClickListener { finish() }
        ImmersionBar.with(this).titleBar(binding.toolbar).init()
    }

    override fun onDestroy() {
        super.onDestroy()
        topOnNativeAd?.let { TopOnNativeAdManager.removeUsedNativeAd(it) }
        TopOnInterAdManager.setManualLoadListener(null)
        nativeAd?.let { AdMobNativeAdManager.removeUsedNativeAd(it) }
    }
}